package sfeir.gwt.ergosoom.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sfeir.gwt.ergosoom.server.service.ProfileService;
import sfeir.gwt.ergosoom.server.util.Pair;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

@SuppressWarnings("serial")
@Singleton
public class PictureServlet extends HttpServlet {
    @Inject
    private Injector injector;

    // @Inject
    // private ProfileService profileService;

    private ProfileService getProfileService() {
        return injector.getInstance(ProfileService.class);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getPathInfo();
        if (email != null && !email.equals("/"))
            email = email.substring(1);
        if (email.endsWith(".png"))
            email = email.substring(0, email.length() - 4);
        if (null != email) {
            Pair<byte[], String> picture = getProfileService().getProfilePicture(email);
            // resp.setContentType(picture.getSecond());
            resp.setContentType("image/png");
            resp.getOutputStream().write(picture.getFirst());
        }
    }
}
